package com.imy.accessibility.node;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.imy.accessibility.CoreService;
import com.imy.accessibility.R;
import com.imy.accessibility.bean.CustomNode;
import com.imy.accessibility.view.BaseNodeViewGroup;
import com.imy.common.util.ALog;
import com.imy.common.util.RefInvoke;
import com.imy.common.util.ToastUtil;
import com.imy.lua.lib.node.UDRule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NodeContentView extends BaseNodeViewGroup {
  private int currentDeep = 1; //层级深度 与索引一致

  private FrameLayout contentRl;
  private ControlUiView controlUiView;


  public NodeContentView(Context context) {
    super(context);
  }

  public NodeContentView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public NodeContentView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override
  protected void init(View inflate) {
    contentRl = inflate.findViewById(R.id.content_rl);
    controlUiView = inflate.findViewById(R.id.control_ui_view);
    controlUiView.setControlUiListener(new ControlUiView.ControlUiListener() {
      @Override
      public void onBackLastNode(CustomNode parent) {
        currentDeep--;
        setNodeInfo(parent);
      }

      @Override
      public void onGoNextNode(CustomNode nodeInfo) {
        //进入一下层级
        currentDeep++;
        setNodeInfo(nodeInfo);
      }

    });
  }

  public void setNodeByList(List<CustomNode> node) {
    contentRl.removeAllViews();
    ALog.ti("setNodeByList :"+node.size());
    for (int i = 0; i < node.size(); i++) {
      CustomNode accessibilityNodeInfo = node.get(i);
      ALog.ti("draw:"+accessibilityNodeInfo);
      drawNodeInfo(accessibilityNodeInfo,i);
    }
  }

  @Override
  protected int getLayoutID() {
    return R.layout.custom_view_node_content;
  }

  private CustomNode mCurrentNodeInfo;


  public void setNodeInfo(CustomNode nodeInfo) {
    //保存当前节点所有的信息
    //先移除所有的View
    this.mCurrentNodeInfo = nodeInfo;
    contentRl.removeAllViews();
    //进入第一层// 复制一个对象

    for (int i = 0; i < nodeInfo.getChildCount; i++) {
      CustomNode child = nodeInfo.child.get(i);
      //开始绘制最顶层
      if (child == null) {
        ALog.ti("发现节点为空:" + "第" + i + "个");
        continue;
      }
      drawNodeInfo(child,i);

    }

  }

  private void drawNodeInfo(CustomNode child, int i) {
    Rect rect = child.mBoundsInScreen;
    NodeView nodeView = new NodeView(mContext);
    int width = rect.right - rect.left;
    int height = rect.bottom - rect.top;
    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, height);
    nodeView.setLayoutParams(layoutParams);
    ALog.ti(" fori child:" + child.mClassName);
    nodeView.setNode(child);
    nodeView.setPosition(i);
    contentRl.addView(nodeView);
    nodeView.setX(rect.left);
    nodeView.setY(rect.top);
    nodeView.setOnChildNodeClicked(new NodeView.OnChildNodeClicked() {
      @Override
      public void onClicked(CustomNode nodeInfo, float x, float y) {
        //判断选中的区域是否有当前节点重叠的存在
        //checkClickedOverlay(nodeInfo, x, y);
        controlUiView.show(nodeInfo);
      }
    });
  }





}
